package Pet.Management.service.impl;

import Pet.Management.domain.entity.PetDemand;
import Pet.Management.repository.PetDemandRepository;
import Pet.Management.service.PetDemandService;
import jakarta.persistence.EntityNotFoundException;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@RequiredArgsConstructor
public class PetDemandServiceImpl implements PetDemandService {

    private final PetDemandRepository petDemandRepository;

    @Override
    @Transactional
    public PetDemand createPetDemand(PetDemand petDemand) {
        return petDemandRepository.save(petDemand);
    }

    @Override
    @Transactional
    public PetDemand updatePetDemand(Long id, PetDemand petDemand) {
        PetDemand existingPetDemand = getPetDemand(id);
        
        existingPetDemand.setPetName(petDemand.getPetName());
        existingPetDemand.setPetType(petDemand.getPetType());
        existingPetDemand.setPetAge(petDemand.getPetAge());
        existingPetDemand.setVaccineRecord(petDemand.getVaccineRecord());
        existingPetDemand.setDietaryHabits(petDemand.getDietaryHabits());
        existingPetDemand.setPetGender(petDemand.getPetGender());
        existingPetDemand.setOwnerRequirements(petDemand.getOwnerRequirements());
        
        return petDemandRepository.save(existingPetDemand);
    }

    @Override
    @Transactional
    public void deletePetDemand(Long id) {
        if (!petDemandRepository.existsById(id)) {
            throw new EntityNotFoundException("Pet demand not found with id: " + id);
        }
        petDemandRepository.deleteById(id);
    }

    @Override
    public PetDemand getPetDemand(Long id) {
        return petDemandRepository.findById(id)
                .orElseThrow(() -> new EntityNotFoundException("Pet demand not found with id: " + id));
    }

    @Override
    public Page<PetDemand> getAllPetDemands(Pageable pageable) {
        return petDemandRepository.findAll(pageable);
    }

    @Override
    public List<PetDemand> getPetDemandsByUserId(Long userId) {
        return petDemandRepository.findByUserId(userId);
    }

    @Override
    public List<PetDemand> searchPetDemands(String petName, String petType, String petGender) {
        Specification<PetDemand> spec = (root, query, cb) -> {
            List<jakarta.persistence.criteria.Predicate> predicates = new ArrayList<>();
            
            if (petName != null && !petName.isEmpty()) {
                predicates.add(cb.like(root.get("petName"), "%" + petName + "%"));
            }
            
            if (petType != null && !petType.isEmpty()) {
                predicates.add(cb.equal(root.get("petType"), petType));
            }
            
            if (petGender != null && !petGender.isEmpty()) {
                predicates.add(cb.equal(root.get("petGender"), petGender));
            }
            
            return cb.and(predicates.toArray(new jakarta.persistence.criteria.Predicate[0]));
        };
        
        return petDemandRepository.findAll(spec);
    }

    @Override
    public PetDemand getDemandById(Long id) {
        return petDemandRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("需求不存在，ID: " + id));
    }
} 